package com.migu.spms.webapi;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.migu.spms.common.StringUtil;
import com.migu.spms.dao.entity.VersionHistoryDO;
import com.migu.spms.dao.inf.IVersionHistoryDao;
import com.migu.spms.service.data.User;
import com.migu.spms.service.inf.IOnlineVersionService;
import com.migu.spms.utils.exception.ServiceException;
import com.migu.spms.webapi.data.UpdateOnlineVersionRequest;

/**
 * 
* 项目名称:  SPMS需求管理
* 包:        com.migu.spms.webapi 
* 类名称:    CreateRequireController.java
* 类描述:    更新上线版本控制类
* 创建人:    fw  
* 创建时间:  2017年6月13日 下午4:29:02
 */
@RestController
@RequestMapping(value = "/onlineVersion", produces = "application/json;charset=UTF-8")
public class OnlineVersionController extends BaseController {
	private  DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Resource(name = "onlineVersionService")
    IOnlineVersionService onlineVersionService;
    
    @Resource(name="IVersionHistoryDao")
    IVersionHistoryDao versionHistoryDao;

    @RequestMapping(value = "/update")
    public String create(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
        User user = getUserBySession(req, sessionId);
        if (user == null) {
            return generateResponse(401);
        }
        UpdateOnlineVersionRequest updateOnlineVersionRequest = ParseMsg(req, UpdateOnlineVersionRequest.class);
        onlineVersionService.update(updateOnlineVersionRequest, user);
        if(StringUtil.isEmpty(updateOnlineVersionRequest.getVersionId().toString())){
        	throw new ServiceException("版本号id为空,请传入versionId字段信息");
        }
        if(StringUtil.isEmpty(updateOnlineVersionRequest.getBundlingFlag())){
        	throw new ServiceException("请传入加解版标识字段信息");
        }
        String bundingFlag=updateOnlineVersionRequest.getBundlingFlag();
        VersionHistoryDO vHistoryDO=new VersionHistoryDO();
        vHistoryDO.setVersion_id(updateOnlineVersionRequest.getVersionId());
        vHistoryDO.setCreator_id(user.getUserId());
        vHistoryDO.setCreator_name(user.getUserName());
        vHistoryDO.setRecord_time(format.format(new Date()));
        if(bundingFlag.equals("1")){
        	vHistoryDO.setRemark("批量绑定需求的期望解决版本");
        }else{
        	vHistoryDO.setRemark("批量解绑需求的期望解决版本");
        }
        vHistoryDO.setOperate_type("01");
		versionHistoryDao.insert(vHistoryDO);
        return generateResponse(200);

    }
}
